package com.example.hospitalmanagementapp.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hospitalmanagementapp.R;
import com.example.hospitalmanagementapp.bean.DepartmentBean;
import com.example.hospitalmanagementapp.bean.SystemBean;

import java.util.List;

/**
 * @author 席
 * 医院系统二级列表——科目
 */
public class DepartmentAdapter extends RecyclerView.Adapter<DepartmentAdapter.DepartmentHolder> {
    private Context context;
    private List<DepartmentBean> departmentStr;
    private DepartmentAdapter.ListenerRecyclerView listener;

    public DepartmentAdapter(Context context) {
        this.context = context;
    }

    public void setDepartmentStr(List<DepartmentBean> departmentStr) {
        this.departmentStr = departmentStr;
    }

    public void setDepartmentListener(ListenerRecyclerView listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public DepartmentHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
            DepartmentHolder departmentHolder=new DepartmentHolder(LayoutInflater.from(context).inflate(R.layout.item_department,null));
        return departmentHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull  DepartmentAdapter.DepartmentHolder holder, int position) {
        holder.departmentName.setText(departmentStr.get(position).getDepartmentName());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null!=listener){
                    //监听器引用
                    listener.onItemClick(position);
                    notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return null==departmentStr ? 0 :departmentStr.size();
    }

    class DepartmentHolder extends RecyclerView.ViewHolder {
        TextView departmentName;
        public DepartmentHolder(@NonNull  View itemView) {
            super(itemView);
            departmentName=itemView.findViewById(R.id.item_department_tv);

        }
    }

    public interface ListenerRecyclerView {
         void onItemClick(int position);
    }

}
